<template>
  <div>
    <!-- 左侧菜单栏：花卉百科分类 -->
    <el-tabs tab-position="left" class="tabs" v-model="activeName" @tab-click="handleClick">

      <!-- 右侧顶部：模糊查询 -->
      <el-form :inline="true" class="demo-form-inline" @submit.native.prevent>
        <el-form-item label="花卉名称">
          <el-input placeholder="请输入关键字" v-model="name" @keyup.native.enter="search"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 左侧菜单项 -- 花卉百科分类 -->
      <el-tab-pane v-for="item in types" :key="item.id" :label="item.typename" :name="(item.id).toString()">
        <!-- 花卉百科列表+分割线 -->
        <el-divider content-position="left">花卉列表</el-divider>
         <!-- 右侧主体内容：花卉百科列表 -->
        <el-table :data="flowers.data" height="65vh" border style="width: 100%" >
          <el-table-column prop="pic" label="花卉图片" width="300" align="center">
            <template slot-scope="scope">
              <img v-if="scope.row.cover.indexOf('http') == -1" :src="URL.HYRJURL + '/' + scope.row.cover" alt="" width="260px" height="150px" >
              <img v-else :src="scope.row.cover" alt="" width="260px" height="150px" >
            </template>
          </el-table-column>
          <el-table-column prop="name" label="花卉名称" width="200" align="center">
            <template slot-scope="scope" >
              {{scope.row.name}}
            </template>
          </el-table-column>
          <el-table-column prop="detail" label="花卉详情" width="400" align="center">
            <template slot-scope="scope">
              <p class="detail">{{scope.row.detail}}</p>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="warning" icon="el-icon-edit" @click="$router.push(`/home/encyclopedias-update/${scope.row.id}`)">修改</el-button>
              <el-button type="primary" icon="el-icon-view" @click="$router.push(`/home/encyclopedias-detail/${scope.row.id}`)">查看详情</el-button>
              <el-button type="danger" icon="el-icon-delete" @click="deleteItem(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <!-- 右侧底部内容：分页 -->
      <br>
      <el-pagination
        v-if="isShow==true"
        background
        :page-size="flowers.pagesize"
        :total="flowers.total"
        @current-change="changeCurrentPage"
        layout="->,prev, pager, next,total">
      </el-pagination>
    </el-tabs>
  </div>
</template>

<script>
import httpApi from '@/http';
import URL from '@/http/BaseUrl'

  export default {
    data() {
      return {
        types:[],//存储花卉类型
        activeName:'10',//存储标签激活项-即type_id,默认为10(春季花卉)
        name:'',//双向数据绑定name关键字输入框
        flowers: {
          data:[],  //花卉列表详情
          pages:0,  //总页数
          page:1, //当前页码
          pagesize:5,  //每页数据量
          total:0 //总数据量
        },
        isShow:true,
        URL,
      }
    },
    methods: {
      // 删除花卉详情列表项
      deleteItem(id){
        httpApi.EncyclopediasApi.delete({id}).then(res=>{
          console.log('花卉详情',res) 
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => { 
            // 提示消息
            this.$message({type: 'success', message: '删除成功!'});
            // 重新渲染
            this.search()
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
        })
      },
      // 获取花卉列表
      getFlowers() {
        httpApi.EncyclopediasApi.queryInfoByTypes({page:this.flowers.page,pagesize:this.flowers.pagesize,type_id:this.activeName}).then(res=>{
        //  console.log('花卉列表',res)
          console.log('花卉列表',res.data.data.data)
          this.flowers.data = res.data.data.data
          this.flowers.pages = res.data.data.pages
          this.flowers.page = res.data.data.page
          this.flowers.pagesize = res.data.data.pagesize
          this.flowers.total = res.data.data.total
        })
      },
      // 点击切换左侧标签栏事件
      handleClick(tab) {
        // 修改当前激活项，即type_id
        this.activeName = tab.name
        // 重新获取花卉列表数据
        this.getFlowers()
      },
      // 点击查询按钮后执行，需要判断输入框中是否有关键字
      search(){
        // trim()去除字符串两端的空白
        if(this.name.trim()==''){//没有关键字
          this.getFlowers()
          this.isShow = true
        }else{//有关键字
          this.listFlowersByName()
          this.isShow = false
        }
      },
      // 通过花卉名称关键字查询花卉百科列表
      listFlowersByName(){
          httpApi.EncyclopediasApi.queryInfoByNameLike({name:this.name}).then(res=>{
          console.log(res)
          this.flowers.data = res.data.data
        })
      },
      // 当当前页改变时触发
      changeCurrentPage(page){
        console.log(page)
        this.flowers.page = page//修改当前页码后，查询电影列表
        this.search()
      },
    },
    // 生命周期方法，页面挂载时
    mounted(){
      // 获取花卉百科类别
      httpApi.EncyclopediasApi.queryTypes().then(res=>{
        console.log(res)
        this.types = res.data.data
      })
      // 页面挂载时，默认加载春季花卉第一页数据
      this.getFlowers() 
      
    }
  }
</script>

<style lang="scss" scoped>
.tabs{
  height: calc(100vh - 100px);
}
.detail{
  display: -webkit-box;
  -webkit-line-clamp:5;
  -webkit-box-orient:vertical;
  overflow:hidden;
  text-overflow:ellipsis;
}
</style>

